<?php
namespace app\common\service;
use \app\common\model\Proxylog as ProxylogModel;
use think\Loader;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/14
 * Time: 15:23
 */
class Proxylog extends Base{
    public function __construct()
    {
        parent::__construct();
        $ProxylogModel =  new ProxylogModel();
        $this->model = $ProxylogModel;
    }
    public function joinProxyService($map,$data){
//        $validate =  Loader::validate('Proxylog');
//        if(!$validate->check($data)){
//            $datas = json_encode((object)null);
//            return ['code'=>3,'msg'=>$validate->getError(),'data'=>$datas];
//        }
        if($data['is_agent']>0){
            $agent_count=Db::name('user')->where(['is_agent'=>array('eq',1),'id'=>array('eq',$map['user_id'])])->count();
            if($agent_count>0){
                return ['code'=>2,'msg'=>'已经申请个人代理过了'];
            }else{
                $map['is_agent'] = 1;
                $map['status'] = array('eq',1);
                $res = $this->model->where($map)->find();
                if($res&&$data['is_agent']==1){
                    return ['code'=>2,'msg'=>'已经申请个人代理过了'];
                }
            }

        }


//            $maps['user_id'] = $map['user_id'];
//            $maps['is_agent'] = 0;
//            $result = $this->model->where($maps)->find();
//            if($result&&$data['is_agent']==0){
//                return ['code'=>3,'msg'=>'已经申请服务商代理'];
//            }


            if($data['server_type']==1){

                $province_count=Db::name('user')->where(['province'=>$data['province'],'server_type'=>array('eq',1)])->count();
                if($province_count>0){
                    return ['code'=>2,'msg'=>'省代理已有存在'];
                }else{
                    $provinceproxy_count=Db::name('proxylog')->where(['province'=>$data['province'],'server_type'=>array('eq',1),'status'=>array('eq',1)])->count();
                    if($provinceproxy_count>0){
                        return ['code'=>2,'msg'=>'省代理已有存在'];
                    }
                }

            }
            if($data['server_type']==2){

                $city_count=Db::name('user')->where(['city'=>$data['city'],'server_type'=>array('eq',2)])->count();
                if($city_count>0){
                    return ['code'=>2,'msg'=>'市代理已有存在'];
                }else{
                    $cityproxy_count=Db::name('proxylog')->where(['city'=>$data['city'],'server_type'=>array('eq',2),'status'=>array('eq',1)])->count();
                    if($cityproxy_count>0){
                        return ['code'=>2,'msg'=>'市代理已有存在'];
                    }
                }

            }

            if($data['server_type']==3){
                $area_count=Db::name('user')->where(['area'=>$data['area'],'server_type'=>array('eq',3)])->count();

                if($area_count>0){
                    return ['code'=>2,'msg'=>'区代理已有存在'];
                }else{
                    $areaproxy_count=Db::name('proxylog')->where(['area'=>$data['area'],'server_type'=>array('eq',3),'status'=>array('eq',1)])->count();

                    if($areaproxy_count>0){
                        return ['code'=>2,'msg'=>'区代理已有存在'];
                    }
                }
            }
            if($data['server_type']==5){
                $area_count=Db::name('user')->where(['regione'=>$data['regione'],'server_type'=>array('eq',5)])->count();
                if($area_count>0){
                    return ['code'=>3,'msg'=>'大区区代理已有存在'];
                }else{
                    $areaproxy_count=Db::name('proxylog')->where(['regione'=>$data['regione'],'server_type'=>array('eq',5),'status'=>array('eq',1)])->count();
                    if($areaproxy_count>0){
                        return ['code'=>3,'msg'=>'大区区代理已有存在'];
                    }
                }
            }
            ;

            $shopwhere['server_type']=array('in',array(1,2,3,5));
            $shopwhere['user_id']=array('eq',$map['user_id']);
            $shopwhere['status']=array('in',array(0));

            $prwhere['is_agent']=array('eq',1);
            $prwhere['user_id']=array('eq',$map['user_id']);
            $prwhere['status']=array('in',array(0));

            $prox_shop=Db::name('proxylog')->where($shopwhere)->find();

            $prox_pr=Db::name('proxylog')->where($prwhere)->find();

            if($data['is_agent']>0){
                if(!empty($prox_pr)){
                    return ['code'=>0,'msg'=>'已经提交申请，正在审核中'];
                }
            }else{
                if(!empty($prox_shop)){
                    return ['code'=>0,'msg'=>'已经提交申请，正在审核中'];
                }
            }



            $data['status']=0;
            $data['createtime']=time();
            unset($data['price']);

            $_datas = $this->model->insertGetId($data);

            if($_datas){
                $data['id']=$_datas;
                return ['code'=>1,'msg'=>'申请成功'];
            }else{
                return ['code'=>0,'msg'=>'代理申请审核中'];
            }

    }

    /**
     *判断是否存在
     * 返回code=2已存在,code=0
     */
    public function IsjoinProxyService($map,$data){
        if($data['is_agent']>0){
            $agent_count=Db::name('user')->where(['is_agent'=>array('eq',1),'id'=>array('eq',$map['user_id'])])->count();
            if($agent_count>0){
                return ['code'=>2,'msg'=>'已经申请个人代理过了'];
            }else{
                $map['is_agent'] = 1;
                $map['status'] = array('eq',1);
                $res = $this->model->where($map)->find();
                if($res&&$data['is_agent']==1){
                    return ['code'=>2,'msg'=>'已经申请个人代理过了'];
                }
            }

        }


        if($data['server_type']==1){

            $province_count=Db::name('user')->where(['province'=>$data['province'],'server_type'=>array('eq',1)])->count();
            if($province_count>0){
                return ['code'=>2,'msg'=>'省代理已有存在'];
            }else{
                $provinceproxy_count=Db::name('proxylog')->where(['province'=>$data['province'],'server_type'=>array('eq',1),'status'=>array('eq',1)])->count();
                if($provinceproxy_count>0){
                    return ['code'=>2,'msg'=>'省代理已有存在'];
                }
            }

        }
        if($data['server_type']==2){

            $city_count=Db::name('user')->where(['city'=>$data['city'],'server_type'=>array('eq',2)])->count();
            if($city_count>0){
                return ['code'=>2,'msg'=>'市代理已有存在'];
            }else{
                $cityproxy_count=Db::name('proxylog')->where(['city'=>$data['city'],'server_type'=>array('eq',2),'status'=>array('eq',1)])->count();
                if($cityproxy_count>0){
                    return ['code'=>2,'msg'=>'市代理已有存在'];
                }
            }

        }

        if($data['server_type']==3){
            $area_count=Db::name('user')->where(['area'=>$data['area'],'server_type'=>array('eq',3)])->count();

            if($area_count>0){
                return ['code'=>2,'msg'=>'区代理已有存在'];
            }else{
                $areaproxy_count=Db::name('proxylog')->where(['area'=>$data['area'],'server_type'=>array('eq',3),'status'=>array('eq',1)])->count();

                if($areaproxy_count>0){
                    return ['code'=>2,'msg'=>'区代理已有存在'];
                }
            }
        }
        if($data['server_type']==5){
            $area_count=Db::name('user')->where(['regione'=>$data['regione'],'server_type'=>array('eq',5)])->count();
            if($area_count>0){
                return ['code'=>3,'msg'=>'大区区代理已有存在'];
            }else{
                $areaproxy_count=Db::name('proxylog')->where(['regione'=>$data['regione'],'server_type'=>array('eq',5),'status'=>array('eq',1)])->count();
                if($areaproxy_count>0){
                    return ['code'=>3,'msg'=>'大区区代理已有存在'];
                }
            }
        }
        ;

        $shopwhere['server_type']=array('in',array(1,2,3,5));
        $shopwhere['user_id']=array('eq',$map['user_id']);
        $shopwhere['status']=array('in',array(0));

        $prwhere['is_agent']=array('eq',1);
        $prwhere['user_id']=array('eq',$map['user_id']);
        $prwhere['status']=array('in',array(0));

        $prox_shop=Db::name('proxylog')->where($shopwhere)->find();

        $prox_pr=Db::name('proxylog')->where($prwhere)->find();

        if($data['is_agent']>0){
            if(!empty($prox_pr)){
                return ['code'=>0,'msg'=>'已经提交申请，正在审核中'];
            }
        }else{
            if(!empty($prox_shop)){
                return ['code'=>0,'msg'=>'已经提交申请，正在审核中'];
            }
        }

        return ['code'=>1,'msg'=>'可以申请'];


    }
}